package linuxbox.admin.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import linuxbox.BoxStatic;
import linuxbox.admin.logic.*;

/**
 * This class displays a user list. 
 */
public class ManagerList extends TagSupport {
	protected String listType = null;
	protected int selected = -1;
	
	public int doStartTag() throws JspException {
		try {
			UserManager um = UserManager.getInstance();
			int[] ids = um.getAllManagers();
			
			JspWriter out = pageContext.getOut();
			
			User user = null;
			StringBuffer retVal = new StringBuffer();
			
			for (int i = 0; i < ids.length; i++) {
				user = um.getUser(ids[i]);
				retVal.append("<OPTION value=" + user.getID());
				
				if (user.getID() == selected) {
					retVal.append(" SELECTED");
				}
				
				retVal.append(">" + user.getLastName() + ", " + user.getFirstName() + "</OPTION>");
			}
			
			out.println(retVal.toString());
			out.flush();
			//out.close();
		} catch(Exception e) {
			BoxStatic.logger.error("Failed to get all users");
			e.printStackTrace();
			throw new JspException(e.getMessage());
		}
		
		return SKIP_BODY;
	}
	
	public int doEndTag() throws JspException {
		return super.doEndTag();
	}

	public void setListType(String aValue) {
		listType = aValue;
	}

	public void setSelected(String aValue) {
		selected = Integer.parseInt(aValue);
	}
}
